/**
 * 
 */
package com.base.myproject.client.busi.storeslist;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author zgw 2009-9-1 上午10:12:53 javazgw@gmail.com
 */
public class StoreinCp extends BaseJDBCPanel {
	static BaseStore<BusiObject> store = null;
	static BaseGrid grid = null;
	final static String busi = SystemMessage.busi[4];

	public StoreinCp() {
		super();
		init();
	}

	protected void init() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeiget(), centerData);

		registerMainStore(store);
		registerStore(store);

		this.setBusino(busi);
		setdefault();
		this.setHeight(600);

	}

	private void setdefault() {
		store.addDefault("busicode", busi);
	}

	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
		panel.setLayout(new BorderLayout());
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150);
		westData.setSplit(true);
		westData.setCollapsible(true);
		westData.setMargins(new Margins(0));
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));
		panel.add(CreateCcodeLeftPanel(), westData);

		TabPanel tabpanel = new TabPanel();
		tabpanel.setWidth(450);
		tabpanel.setHeight(450);
		tabpanel.setAutoHeight(true);
		TabItem normal = new TabItem("基本信息");

		VerticalPanel verticalpanel = new VerticalPanel();
		verticalpanel.add(createStoreInPreGrid());
		verticalpanel.add(CreateGCodePanel());
		normal.add(verticalpanel);
		/*
		 * TabItem gcode = new TabItem("商品信息"); gcode.add(CreateGCodePanel());
		 */
		tabpanel.add(normal);
		// tabpanel.add(gcode);
		panel.add(tabpanel, centerData);

		return panel;
	}

	/**
	 * 合同管理左边查询
	 * 
	 * @return
	 */
	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		ColumnConfig cname = new ColumnConfig("codename", "批次", 200);
		TextField<String> gnametext = new TextField<String>();

		columns.add(cname);
		DateField dateField = new DateField();

		ColumnModel cm = new ColumnModel(columns);

		// --set modetype
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.addField("busicode");
		type.addField("bcode");
		type.addField("ccode");
		type.addField("busistatus");
		type.addField("vprepare");
		type.addField("predate");
		type.addField("modifydate");
		type.addField("codename");
		type.addField("outordercode");
		type.addField("subscribedate");
		type.addField("odate");
		type.addField("backodate");
		type.addField("ncode");
		type.addField("ccodecommisionh");
		type.addField("signsite");
		type.addField("priceterm");
		type.addField("targport");
		type.addField("targbport");
		type.addField("shipmode");
		type.addField("tarplace");
		type.addField("markdesc");
		type.addField("mark");
		// type.addField("Phone");
		// type.addField("imagepath");
		StoreInfo info = new StoreInfo();
		info.setTablename("storeslist");
		info.setSql("select * from storeslist where busicode ='" + busi + "'");
		info.setKeyname(new String[] { "icode" });

		BaseBusiLeftPanel basbusileftpanel = new BaseBusiLeftPanel(columns,
				type, info);
		store = basbusileftpanel.getStore();

		basbusileftpanel.getGrid().setAutoExpandColumn("codename");
		setMaincomponent(basbusileftpanel.getGrid());
		return basbusileftpanel.getPanel();
	}

	
	
	/**
	 * 商品
	 * 
	 * @return
	 */
	private BaseGrid CreateGCodePanel() {

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig cgcode = new ColumnConfig("cgcode", "公司货号", 100);
		TextField<String> cgcodetext = new TextField<String>();
		cgcode.setEditor(new CellEditor(cgcodetext));
		columns.add(cgcode);

		ColumnConfig user_cpms = new ColumnConfig("user_cpms", "产品描述", 100);
		TextField<String> user_cpmstext = new TextField<String>();
		user_cpms.setEditor(new CellEditor(user_cpmstext));
		columns.add(user_cpms);
		ColumnConfig user_bzsm = new ColumnConfig("user_bzsm", "包装说明", 100);
		TextField<String> user_bzsmtext = new TextField<String>();
		user_bzsm.setEditor(new CellEditor(user_bzsmtext));
		columns.add(user_bzsm);
		ColumnConfig qty = new ColumnConfig("qty", "数量", 100);
		TextField<String> qtytext = new TextField<String>();
		qty.setEditor(new CellEditor(qtytext));
		columns.add(qty);
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 100);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		columns.add(qtyunit);
		ColumnConfig upric = new ColumnConfig("upric", "单价", 100);
		TextField<String> uprictext = new TextField<String>();
		upric.setEditor(new CellEditor(uprictext));
		columns.add(upric);

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("upicode");
		type.addField("codename");
		type.addField("cgicode");
		type.addField("cgcode");
		type.addField("gcode");
		type.addField("gcode");
		type.addField("qty");
		type.addField("modeltype");
		type.addField("qtyunit");
		type.addField("upric");
		type.addField("scode");
		type.addField("buyprice");
		type.addField("saleprice");
		type.getField("qty").setType(Double.class);
		type.getField("buyprice").setType(Double.class);
		type.getField("saleprice").setType(Double.class);
		 type.addField("deeldate");
		 type.getField("deeldate").setType(Date.class);

		StoreInfo info = new StoreInfo();

		info.setTablename("storeslistg");
		info.setSql("select * from storeslistg");
		info.setParent(store);// 关联主表
	//	info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		info.setKeyname(new String[] { "upicode"}); // 主键

		BaseStore contractgcode = new BaseStore<JDBCModelData>(type, info);
		contractgcode.setName("contractgcode");
		ColumnModel cm = new ColumnModel(columns);
		BaseGrid contractgcodegrid = new BaseGrid(contractgcode, cm);

		cbs.setSelectionMode(SelectionMode.MULTI);

		contractgcodegrid.setSelectionModel(cbs);
//		contractgcodegrid.setAutoHeight(true);
//		contractgcodegrid.setAutoWidth(true);
		contractgcodegrid.setHeight(200);
		contractgcodegrid.setWidth(800);
		registerStore(contractgcode);
		return contractgcodegrid;
	}

	BaseGrid storeinpregcode = null;

	BaseGrid createStoreInPreGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("idx", "idx", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("codename", "codename", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("ccode", "ccode", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("bcode", "bcode", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("cgicode", "cgicode", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("cgcode", "cgcode", 80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("gcode", "gcode", 80);
		columns.add(C8);
		ColumnConfig C9 = new ColumnConfig("scode", "scode", 80);
		columns.add(C9);
		ColumnConfig C10 = new ColumnConfig("qty", "qty", 80);
		columns.add(C10);
		ColumnConfig C11 = new ColumnConfig("qtyunit", "qtyunit", 80);
		columns.add(C11);
		ColumnConfig C12 = new ColumnConfig("qtp", "qtp", 80);
		columns.add(C12);
		ColumnConfig C13 = new ColumnConfig("qtpunit", "qtpunit", 80);
		columns.add(C13);
		ColumnConfig C14 = new ColumnConfig("upric", "upric", 80);
		columns.add(C14);
		ColumnConfig C15 = new ColumnConfig("inoutflag", "inoutflag", 80);
		columns.add(C15);
		ColumnConfig C16 = new ColumnConfig("upicode", "upicode", 80);
		columns.add(C16);
		ColumnConfig C17 = new ColumnConfig("busicode", "busicode", 80);
		columns.add(C17);
		ColumnConfig C18 = new ColumnConfig("modeltype", "modeltype", 80);
		columns.add(C18);
		ColumnConfig C19 = new ColumnConfig("upscode", "upscode", 80);
		columns.add(C19);
		ColumnConfig C20 = new ColumnConfig("buyprice", "buyprice", 80);
		columns.add(C20);
		ColumnConfig C21 = new ColumnConfig("saleprice", "saleprice", 80);
		columns.add(C21);
		ColumnConfig C22 = new ColumnConfig("deeldate", "deeldate", 80);
		columns.add(C22);
		ColumnConfig C23 = new ColumnConfig("originalicode", "originalicode",
				80);
		columns.add(C23);
		ColumnConfig C24 = new ColumnConfig("copybusicode", "copybusicode", 80);
		columns.add(C24);
		ColumnConfig C25 = new ColumnConfig("copyicode", "copyicode", 80);
		columns.add(C25);
		ColumnConfig C26 = new ColumnConfig("pronum", "pronum", 80);
		columns.add(C26);
		ColumnConfig C27 = new ColumnConfig("buypric1", "buypric1", 80);
		columns.add(C27);
		ColumnConfig C28 = new ColumnConfig("buypric2", "buypric2", 80);
		columns.add(C28);
		ColumnConfig C29 = new ColumnConfig("buypric3", "buypric3", 80);
		columns.add(C29);
		ColumnConfig C30 = new ColumnConfig("saleprice1", "saleprice1", 80);
		columns.add(C30);
		ColumnConfig C31 = new ColumnConfig("saleprice2", "saleprice2", 80);
		columns.add(C31);
		ColumnConfig C32 = new ColumnConfig("saleprice3", "saleprice3", 80);
		columns.add(C32);
		ColumnConfig C33 = new ColumnConfig("goodsstatus", "goodsstatus", 80);
		columns.add(C33);

		ColumnModel cm = new ColumnModel(columns);
		storeinpregcode = new BaseGrid(getStore(), cm,false,false);
		cbs.setSelectionMode(SelectionMode.MULTI);
		storeinpregcode.setSelectionModel(cbs);
		storeinpregcode.setHeight(200);
		storeinpregcode.setWidth(800);
		return storeinpregcode;
	}

	BaseStore<BusiObject> storeinpregcode_store = null;

	BaseStore<BusiObject> getStore() {
		if (storeinpregcode_store == null) {
			createStore();
		}
		return storeinpregcode_store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("idx");
		type.getField("idx").setType(String.class);
		type.addField("codename");
		type.getField("codename").setType(String.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("cgicode");
		type.getField("cgicode").setType(String.class);
		type.addField("cgcode");
		type.getField("cgcode").setType(String.class);
		type.addField("gcode");
		type.getField("gcode").setType(String.class);
		type.addField("scode");
		type.getField("scode").setType(String.class);
		type.addField("qty");
		type.getField("qty").setType(Double.class);
		type.addField("qtyunit");
		type.getField("qtyunit").setType(String.class);
		type.addField("qtp");
		type.getField("qtp").setType(String.class);
		type.addField("qtpunit");
		type.getField("qtpunit").setType(String.class);
		type.addField("upric");
		type.getField("upric").setType(Double.class);
		type.addField("inoutflag");
		type.getField("inoutflag").setType(Integer.class);
		type.addField("upicode");
		type.getField("upicode").setType(Long.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("modeltype");
		type.getField("modeltype").setType(String.class);
		type.addField("upscode");
		type.getField("upscode").setType(String.class);
		type.addField("buyprice");
		type.getField("buyprice").setType(Double.class);
		type.addField("saleprice");
		type.getField("saleprice").setType(Double.class);
		type.addField("deeldate");
		type.getField("deeldate").setType(Date.class);
		type.addField("originalicode");
		type.getField("originalicode").setType(Long.class);
		type.addField("copybusicode");
		type.getField("copybusicode").setType(String.class);
		type.addField("copyicode");
		type.getField("copyicode").setType(Long.class);
		type.addField("pronum");
		type.getField("pronum").setType(String.class);
		type.addField("buypric1");
		type.getField("buypric1").setType(Double.class);
		type.addField("buypric2");
		type.getField("buypric2").setType(Double.class);
		type.addField("buypric3");
		type.getField("buypric3").setType(Double.class);
		type.addField("saleprice1");
		type.getField("saleprice1").setType(Double.class);
		type.addField("saleprice2");
		type.getField("saleprice2").setType(Double.class);
		type.addField("saleprice3");
		type.getField("saleprice3").setType(Double.class);
		type.addField("goodsstatus");
		type.getField("goodsstatus").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from storeslistg");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		storeinpregcode_store = new BaseStore<BusiObject>(type, info);
		return storeinpregcode_store;
	}
	
/*	Widget tt ()
	{
		
		
	}*/
}